package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class DishServiceImpl implements DishService {

    private final DishMapper dishMapper;
    private final DishFlavorMapper dishFlavorMapper;
    private final SetmealDishMapper setmealDishMapper;
    private final SetmealMapper setmealMapper;


    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);
//获取insert语句的主键值
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }


    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            Dish dish=dishMapper.getById(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> setmealIds=setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds!=null&&setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

//        for (Long id : ids) {
//            dishMapper.deleteById(id);
//            dishFlavorMapper.deleteById(id);
//
//        }
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByIds(ids);
    }

    @Override
    public DishVO getByIdwithFlavor(Long id) {
        Dish dish=dishMapper.getById(id);

        List<DishFlavor> dishFlavors=dishFlavorMapper.getByDishId(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //创建Dish对象
        Dish dish=new Dish();
        //将DishDTO中的属性值复制到Dish对象中
        BeanUtils.copyProperties(dishDTO,dish);

        //更新Dish表中的数据
        dishMapper.update(dish);

        //根据DishDTO中的id删除DishFlavor表中的数据
        dishFlavorMapper.deleteById(dishDTO.getId());

        //获取DishDTO中的口味列表
        List<DishFlavor> flavors = dishDTO.getFlavors();
//        //向口味表插入n条数据
//        dishFlavorMapper.insertBatch(flavors);
        //如果口味列表不为空且长度大于0
        if (flavors!=null && flavors.size()>0){
            //遍历口味列表
            flavors.forEach(dishFlavor -> {
                //设置DishFlavor中的DishId为DishDTO中的id
                dishFlavor.setDishId(dishDTO.getId());
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    // 重写list方法，根据categoryId查询菜品列表
    public List<Dish> list(Long categoryId) {
        // 创建Dish对象，设置categoryId和status属性
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        // 调用dishMapper的list方法，根据Dish对象查询菜品列表
        return dishMapper.list(dish);
    }
    public List<DishVO> listWithFlavor(Dish dish) {
        //根据菜品查询菜品列表
        List<Dish> dishList = dishMapper.list(dish);

        //创建一个DishVO列表
        List<DishVO> dishVOList = new ArrayList<>();

        //遍历菜品列表
        for (Dish d : dishList) {
            //创建一个DishVO对象
            DishVO dishVO = new DishVO();
            //将Dish对象中的属性值复制到DishVO对象中
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            //将口味设置到DishVO对象中
            dishVO.setFlavors(flavors);
            //将DishVO对象添加到DishVO列表中
            dishVOList.add(dishVO);
        }

        //返回DishVO列表
        return dishVOList;
    }

    @Transactional
    @Override
    public void starOrStop(Integer status, Long id) {
        // 根据传入的status和id创建Dish对象
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        // 更新Dish对象
        dishMapper.update(dish);

        // 如果是停售操作
        if (status == StatusConstant.DISABLE) {
            // 如果是停售操作，还需要将包含当前菜品的套餐也停售
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            // select setmeal_id from setmeal_dish where dish_id in (?,?,?)
            List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
            if (setmealIds != null && setmealIds.size() > 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

//    @Override
//    public void starOrStop(Integer status, Long id) {
//
//    }

//
//    @Override
//    public void starOrStop(Integer status, Long id) {
//
//        if(status == StatusConstant.ENABLE) {
//            //select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
//            List<Dish> dishList = dishMapper.getBySetmealId(id);
//            if (dishList != null && dishList.size() > 0) {
//                dishList.forEach(dish -> {
//                    if (StatusConstant.DISABLE == dish.getStatus()) {
//                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
//                    }
//                });
//            }
//        }
//    }




}
